<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>中心领导管理</title>
    <link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
    <script src="../../res/js/jquery-3.1.1.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../res/layui/layui.js"></script>
    <script src="../../res/js/Tools.js"></script>
    <link rel="stylesheet" type="text/css" href="../AtsEmpCSS/atsEmpCss.css"/>
</head>
<body>
    <div class="demoTable">
        <div class="layui-inline">
            <input class="layui-input" name="selectTitle" placeholder="请输入领导名称" id="selectTitle" autocomplete="off">
        </div>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" id="startTimes" name="startTime" autocomplete="off" autocomplete="off" placeholder="请输入开始日期" >
        </div>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" id="endTimes" name="endTime" autocomplete="off" autocomplete="off" placeholder="请输入结束日期" >
        </div>
        <input type="button" id="seachTable" class="layui-btn layui-btn-sm" value="搜索" />
    </div>
    <table id="demo" lay-filter="test" ></table>
    <script>
        var tableTwo = null;
        var table = null;
        layui.use('table', function() {
            var table = layui.table

            tableTwo=table.render({
                elem: '#demo'
                ,height: 500
                ,toolbar: '#toolbarDemo'
                ,url: '../../UserseControllerLeader/selectAll' //数据接口
                ,page: true //开启分页
                ,cols: [[ //表头
                    {type: 'checkbox', fixed: 'left'}
                    ,{field: 'leaderName', title: '领导名称', align:'center'}
                    ,{field: 'leaderBusiness', title: '负责事务', align:'center'}
                    ,{field: 'leaderExp', title: '职责说明', align:'center'}
                    ,{field: 'phone', title: '联系方式', align:'center'}
                    ,{title: '操作',fixed: 'right', width:200, align:'center', toolbar: '#barDemo'}
                ]]
            });
            $(function(){
                $("#seachTable").on("click",function(){
                    //var name = document.getElementById("selectTitle").value;
                    //alert(name);
                    //执行重载
                    //带条件查询
                    tableTwo.reload({
                        where: {
                            leaderName: $("#selectTitle").val(),
                            startTime:$("#startTimes").val(),
                            endTime:$("#endTimes").val(),
                        },
                        page: { curr: 1 }
                    });
                    rederTime();
                })
            });
            table.on('tool(test)', function(obj){
                var data = obj.data;//行数据
                //console.log(obj)
                if(obj.event === 'del'){
                    layer.confirm('您真的要删除吗?', function(index){
                        obj.del();
                        layer.close(index);
                        //使用ajax进入后台删除数据库中的数据
                        var url = "../../UserseControllerLeader/delete";
                        var data$ = {leaderId:data.leaderId};
                        //alert(data.leaderId);
                        $.post(url,data$,function(obj){
                            //提示
                            layer.msg(obj.msg);
                            //刷新父页面
                            parent.layui.table.reload("demo");
                        },"json");
                    });

                } else if(obj.event === 'edit'){
                    //alert(leaderId);
                    var data = obj.data;//行数据

                    //iframe层
                    layer.open({
                        type: 2,
                        title: '数据修改页面',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['64%', '63%'],
                        content: '../../pages/AtsEmpLeader/OperAtionLeader.html?id='+data.leaderId, //iframe的url
                        end : function() {
                           // parent.layui.table.reload("demo");
                        }
                    });
                } else if(obj.event === 'more'){
                    var data = obj.data;//行数据
                    //iframe层
                    layer.open({
                        type: 2,
                        title: '更多信息',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['64%', '54%'],
                        content: '../../pages/AtsEmpLeader/MoreAtsEmpLeader.html?id='+data.leaderId, //iframe的url
                        /*end : function() {
                            tableTwo.reload();
                        }*/
                    });

                } else if(obj.event === 'export'){
                    var url = "../../UserseControllerExpert/exportWord?type=4&id=" + data.leaderId;
                    location.href = url;
                }
            });
            //头工具栏事件
            table.on('toolbar(test)', function(obj){
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){

                    case 'addBtn':
                        //iframe层
                        layer.open({
                            type: 2,
                            title: '数据新增页面',
                            shadeClose: true,
                            shade: 0.8,
                            area: ['64%', '63%'],
                            content: '../../pages/AtsEmpLeader/OperAtionLeader.html', //iframe的url
                            end : function() {
                                //parent.layui.table.reload("demo");
                            }
                        });
                        break;
                    case 'export':
                        var data = checkStatus.data;
                        var ids = "";

                        $.each(data, function(index, item) {
                            ids += item.leaderId + ",";
                        });

                        if(ids.length==0){
                            layer.msg("请选择需要操作的行");
                            return;
                        }
                        $.each(data, function(index, item) {
                            setTimeout(function(){
                                var url = "../../UserseControllerExpert/exportWord?type=4&id=" + item.leaderId;
                                window.open(url);
                            },1000)
                        });
                        break;
                    case 'delMany':

                        var data = checkStatus.data;
                        var ids = "";
                        for (var i = 0; i < data.length; i++) {
                            ids += data[i].leaderId + ",";
                        }
                        ids = ids.slice(0, -1);
                        console.info(ids);
                        if(ids==null||ids==""){
                            layer.msg("请至少选择一行！");
                            return;
                        }else{
                            layer.confirm('您真的要删除吗?', function(index) {
                                var url = "../../UserseControllerLeader/deleteMany";
                                var datas = {leaderIds: ids};
                                $.post(url, datas, function (objs) {
                                    if (objs.num == 1) {
                                        layer.msg(objs.msg, {
                                            icon: 6,
                                            time: 500
                                        }, function () {
                                            //window.location.reload();
                                            //window.location.reload();
                                            window.location.reload();
                                        });
                                    } else {
                                        layer.msg(objs.msg, {
                                            icon: 2,
                                            time: 500
                                        }, function () {
                                            return;
                                        });
                                    }
                                }, "json");
                            });
                        }
                };
            });
            //刷新时间
            layui.use('laydate', function () {
                laydate = layui.laydate;
                //开始时间、结束时间
                laydate.render({
                    elem: '#startTimes',
                    trigger: 'click'
                });

                laydate.render({
                    elem: '#endTimes',
                    trigger: 'click'
                });
            });

            /**
             * 刷新时间控件
             */
            function rederTime() {
                laydate.render({
                    elem: '#startTimes',
                    trigger: 'click'
                });

                laydate.render({
                    elem: '#endTimes',
                    trigger: 'click'
                });
            }
        });
    </script>
    <script>

    </script>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container" id="insertTable">
            <a class="layui-btn layui-btn-sm" lay-event="addBtn">新增</a>
            <a class="layui-btn layui-btn-sm" id="delMany" lay-event="delMany"  data-type="reload">批量删除</a>
            <a class="layui-btn layui-btn-sm" lay-event="export">批量导出</a>
        </div>
    </script>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="more">查看</a>
        <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="export">导出</a>
    </script>
</body>
</html>